package com.sakura.random_team;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;

public class Window extends JFrame {
    public final static int SIZE_X = 1600;
    public final static int SIZE_Y = 900;

    private String[] playerList = new String[Main.maxPlayerCount];
    private int playerListIndex = 0;
    public String[][] finalTeams;

    public Window(){

        JButton startRandom = new JButton("Start Random");
        startRandom.setBounds(getComponentCenterFor(160, 90, SIZE_X / 2, SIZE_Y / 2));
        startRandom.setFont(new Font("Microsoft YaHei", Font.PLAIN, 18));
        startRandom.setBackground(Color.PINK);

        JButton addPlayer = new JButton("Add");
        addPlayer.setBounds(getComponentCenterFor(175, 50, SIZE_X / 2 + 240, SIZE_Y / 2 - 210));
        addPlayer.setFont(new Font("Microsoft YaHei", Font.PLAIN, 18));
        addPlayer.setBackground(Color.PINK);

        JButton deletePlayer = new JButton("Delete Last");
        deletePlayer.setBounds(getComponentCenterFor(175, 50, SIZE_X / 2 - 240, SIZE_Y / 2 - 210));
        deletePlayer.setFont(new Font("Microsoft YaHei", Font.PLAIN, 18));
        deletePlayer.setBackground(Color.PINK);

        JButton deleteAllPlayer = new JButton("Delete All");
        deleteAllPlayer.setBounds(getComponentCenterFor(175, 50, SIZE_X / 2 , SIZE_Y / 2 - 210));
        deleteAllPlayer.setFont(new Font("Microsoft YaHei", Font.PLAIN, 18));
        deleteAllPlayer.setBackground(Color.PINK);

        JTextField inputTeamCount = new JTextField("请输入队伍数量. ");
        inputTeamCount.setBounds(getComponentCenterFor(300, 75, SIZE_X / 2, SIZE_Y / 2 - 150));
        inputTeamCount.setFont(new Font("Microsoft YaHei", Font.BOLD, 35));

        JTextField inputPlayer = new JTextField("请输入玩家. ");
        inputPlayer.setBounds(getComponentCenterFor(650, 125, SIZE_X / 2, SIZE_Y / 2 - 300));
        inputPlayer.setFont(new Font("Microsoft YaHei", Font.ITALIC + Font.BOLD, 44));

        JTextArea TeamA = new JTextArea("Team A");
        TeamA.setBounds(getComponentCenterFor(300, 600, SIZE_X / 2 - 500, SIZE_Y / 2 -100));
        TeamA.setFont(new Font("Microsoft YaHei", Font.BOLD, 25));
        TeamA.setBackground(new Color(0xEC5959));
        TeamA.setEnabled(false);
        TeamA.setAlignmentX(CENTER_ALIGNMENT);

        JTextArea TeamB = new JTextArea("Team B");
        TeamB.setBounds(getComponentCenterFor(300, 600, SIZE_X / 2 + 500, SIZE_Y / 2 -100));
        TeamB.setFont(new Font("Microsoft YaHei", Font.BOLD, 25));
        TeamB.setBackground(new Color(0x5276EE));
        TeamB.setEnabled(false);
        TeamB.setAlignmentX(CENTER_ALIGNMENT);

        inputTeamCount.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                if (inputTeamCount.getText().equals("请输入队伍数量. ")){
                    inputTeamCount.setText("");
                }
            }

            @Override
            public void focusLost(FocusEvent e) {
                inputTeamCount.setForeground(Color.BLACK);
                if (inputTeamCount.getText().isEmpty()) inputTeamCount.setText("请输入队伍数量. ");
            }
        });

        inputPlayer.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                inputPlayer.setText("");
            }

            @Override
            public void focusLost(FocusEvent e) {
                if (inputPlayer.getText().isEmpty()) inputPlayer.setText("请输入玩家. ");
            }
        });

        inputPlayer.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER){
                    addPlayer.doClick();
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {

            }
        });

        startRandom.addActionListener(e -> {
            if(inputTeamCount.getText().isEmpty() || inputTeamCount.getText().equals("请输入队伍数量. ") || inputPlayer.getText().isEmpty() || inputPlayer.getText().equals("请输入玩家列表, 用\" ; \" 分隔. ")) return;
            String text = inputTeamCount.getText();
            int teamCount;
            try {
                teamCount = Integer.parseInt(text);
            }catch (NumberFormatException e1){
                inputTeamCount.setText("sb");
                inputTeamCount.setForeground(Color.RED);
                return;
            }
            int k = 0;
            while (playerList[k] != null){
                k++;
            }
            String[] finalPlayerList = new String[k];
            for (int i = 0; i < k; i++) {
                finalPlayerList[i] = playerList[i];
                System.out.println(Arrays.toString(finalPlayerList));
            }
            DivideSystem divideSystem = new DivideSystem(teamCount, finalPlayerList);
            finalTeams = divideSystem.getResult().clone();
            System.out.println(Arrays.deepToString(finalTeams));
            System.out.println("❀");
            setTeamLabel(TeamA, TeamB);
        });

        addPlayer.addActionListener(e -> {
            if(!inputPlayer.getText().isEmpty()){
                playerList[playerListIndex] = inputPlayer.getText();
                playerListIndex++;
                inputPlayer.setText("");
            }
            System.out.println(Arrays.toString(playerList));
        });

        deletePlayer.addActionListener(e -> {
            if (playerListIndex >= 1){
                playerListIndex--;
                playerList[playerListIndex] = null;
            }
            System.out.println(Arrays.toString(playerList));
        });

        deleteAllPlayer.addActionListener(e -> {
            playerList = new String[Main.maxPlayerCount];
            playerListIndex = 0;
            System.out.println(Arrays.toString(playerList));
        });

        Container container = getContentPane();
        container.setLayout(null);
        container.setBackground(new Color(0xFFAAFF));
        container.add(startRandom);
        container.add(inputTeamCount);
        container.add(inputPlayer);
        container.add(TeamA);
        container.add(TeamB);
        container.add(addPlayer);
        container.add(deletePlayer);
        container.add(deleteAllPlayer);


        setTitle("❀Random Team Tool❀");
        setSize(SIZE_X, SIZE_Y);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        setLocationRelativeTo(null);
    }

    private Rectangle getComponentCenterFor(int width, int height, int centerX, int centerY){
        return new Rectangle(centerX - width / 2, centerY - height / 2, width, height);
    }

    private void clearLabel(JTextArea... jTextAreas){
        for (JTextArea jTextArea : jTextAreas){
            jTextArea.setText("");
        }
    }

    private void setTeamLabel(JTextArea... jTextAreas){
        int k = 0;
        for (JTextArea jTextArea : jTextAreas) {
            clearLabel(jTextArea);
            int i = 0;
            while (this.finalTeams[k][i] != null){
                jTextArea.setText(jTextArea.getText() + "\n" + finalTeams[k][i]);
                i++;
            }
            k++;
        }
    }
}
